package com.giggle.nacos;

import com.alibaba.cloud.dubbo.registry.SpringCloudRegistryFactory;
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.ribbon.ConditionalOnRibbonNacos;
import com.giggle.common.helper.ReflectHelper;
import com.giggle.nacos.discovery.NacosRibbonExtClientConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.logger.Level;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * @author guozichen
 * @ClassName:
 * @Description: (dubbo自动配置)
 * @date 2020/8/26 11:03
 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnBean({SpringClientFactory.class})
@ConditionalOnRibbonNacos
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureAfter({RibbonAutoConfiguration.class})
@RibbonClients(
        defaultConfiguration = {NacosRibbonExtClientConfiguration.class}
)
public class DubboAutoConfiguration {
    public DubboAutoConfiguration() {
    }

    @Bean(name = {"applicationContext"})
    @ConditionalOnClass({SpringCloudRegistryFactory.class, SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class})
    public ConfigurableApplicationContext configurableApplicationContext() {
        return (ConfigurableApplicationContext)ReflectHelper.getStaticField(SpringCloudRegistryFactory.class, "applicationContext");
    }

    @Bean
    public Level level() {
        Level level = Level.INFO;
        String envLevel = System.getProperty("giggle.env.logger.level");
        if (StringUtils.isNotBlank(envLevel)) {
            if (Level.INFO.toString().equalsIgnoreCase(envLevel)) {
                level = Level.INFO;
            }

            if (Level.DEBUG.toString().equalsIgnoreCase(envLevel)) {
                level = Level.DEBUG;
            }
        }

        return level;
    }

    @Bean
    public File file() {
        String dirPath = System.getProperty("giggle.log.workdir");
        if (StringUtils.isBlank(dirPath)) {
            dirPath = this.getClass().getClassLoader().getResource("").getPath();
        }

        dirPath = dirPath.concat(File.separator).concat("dubbo.log");
        return new File(dirPath);
    }
}
